package com.sohu.pickoo.frag;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import com.sohu.csync.util.CsyncLog;
import com.sohu.peak.gallery.domain.Constants;
import com.sohu.peak.util.Logger;

public class FragService {
	private static final Logger log = new Logger();
	/*
	 * 使用httpclient生成碎片
	 */
	public static boolean createFrag(String url, String file)
	{
		log.debug("url="+url);
		log.debug("file="+file);
		HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        int statusCode;
        try {
        	statusCode = client.executeMethod(method);
        	if(statusCode!=HttpStatus.SC_OK){
        		log.debug("错误返回码="+statusCode);
        		return false;
        	}
        	if(!strToFile(method.getResponseBodyAsString(),file))      		
        		return false;
        } catch (HttpException e) {
            // TODO Auto-generated catch block
        	log.error("HTTP错误"+e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
        	log.error("IO错误"+e.getMessage());
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }catch(Exception e){
        	log.error("错误"+e.getMessage());
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;      	
        }finally{
        	method.releaseConnection();
        }
		return true;
	}
	
	public static boolean strToFile(String str,String file)
	{
		log.debug("返回字符串："+str);
		if(str.trim().equals(""))		
			return false;
		String path = file.substring(0, file.lastIndexOf("/"));
		log.debug("path："+path);		
		try   
		{
			if(!(new File(path)).exists()){ 
				if(!(new File(path)).mkdirs()){
					log.debug("创建目录失败");
					return false;
				}
			}
			CsyncLog.scyncRecord("+", file);
		  FileOutputStream fout =new FileOutputStream(file);   
		  fout.write(str.getBytes());   
		  fout.close();   
		}   
		catch(IOException e)
		{
			log.error(e.getMessage());
			return false;
		}
		return true;
	}
	
	public static String getRealTarget(String target)
	{
		String filename = null;
		String fullFilePath = null;
		if(target.contains(".inc"))
		{
			filename = target.substring(target.indexOf("/"),target.indexOf(".inc")+4);
			fullFilePath = Constants.incPath+filename;
		}
		else if(target.contains(".xml"))
		{
			filename = target.substring(target.indexOf("/"));
			fullFilePath = Constants.xmlPath+filename;
		}
		else if(target.contains(".shtml"))
		{
			filename = target.substring(target.indexOf("/"),target.indexOf(".shtml")+6);
			fullFilePath = Constants.incPath+filename;
		}
		//log.debug("截取的碎片文件："+filename+" 全路径："+fullFilePath);		
		return fullFilePath;
	}
}
